package com.wq.httpclientcore.utils;

import io.micrometer.common.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class CommonUtils {

    public static List<Long> toLongIdList(String ids) {
        return toLongIdList(ids, null);
    }

    public static List<Long> toLongIdList(String ids, Set<String> exclude) {
        String[] idArray = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (int i = 0, length = idArray.length; i < length; i++) {
            if (exclude != null && exclude.contains(idArray[i]))
                continue;
            idList.add(Long.parseLong(idArray[i]));
        }
        return idList;
    }

    public static List<String> toStringIdList(List<Long> longIdList) {
        if (longIdList == null) {
            return null;
        }
        if (longIdList.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> stringIdList = longIdList.stream().map(item -> item.toString()).collect(Collectors.toList());
        return stringIdList;
    }

    public static Boolean checkIdString(String ids) {
        if (StringUtils.isBlank(ids))
            return false;
        return ids.matches("^[0-9,]+$");
    }

    /**
     * @param list     分页前的集合
     * @param pageNum  页码
     * @param pageSize 页数
     * @param <T>
     * @return 分页后的集合
     */
    public static <T> List<T> pageList(List<T> list, int pageNum, int pageSize) {
        //计算总页数
        int page = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
        if (page < pageNum)
            return new ArrayList<>();
        //兼容性分页参数错误
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageNum = pageNum >= page ? page : pageNum;
        // 开始索引
        int begin = 0;
        // 结束索引
        int end = 0;
        if (pageNum != page) {
            begin = (pageNum - 1) * pageSize;
            end = begin + pageSize;
        } else {
            begin = (pageNum - 1) * pageSize;
            end = list.size();
        }
        return list.subList(begin, end);
    }

    /**
     * 下划线转驼峰命名
     */
    public static String toUnderScoreCase(String str) {
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1)) {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }

            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                sb.append("_");
            } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
                sb.append("_");
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    public static List<String> long2String(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(String::valueOf).toList();
    }

    public static List<Long> string2Long(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        try {
            return list.stream().map(Long::valueOf).toList();
        } catch (Exception e) {
            return null;
        }
    }
}
